package com.zjm.json;

import com.google.gson.Gson;
import com.zjm.json.entity.Person;
import com.zjm.json.util.GsonUtils;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ZJM
 * @date 2024/4/18 21:31
 */
public class GsonTest01 {

    @Test
    public void test() {
        String json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        // 解析JSON
        Gson gson = new Gson();
        Map map = gson.fromJson(json, Map.class);

        // 输出解析结果
        System.out.println(map.get("name"));
        System.out.println(map.get("age"));
        System.out.println(map.get("city"));
    }

    @Test
    public void test01() {
        String json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";

        // 将JSON字符串转换为Map
        Map<String, Object> map = GsonUtils.jsonToMap(json);

        // 输出Map内容
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // 创建一个Map
        Map<String, Object> newMap = new HashMap<>();
        newMap.put("name", "Alice");
        newMap.put("age", 25);
        newMap.put("city", "Los Angeles");

        // 将Map转换为JSON字符串
        String newJson = GsonUtils.mapToJson(newMap);
        System.out.println(newJson);
    }
}
